﻿using Red.Controls.ViewModels;
using Red.EngineData.View;
using Red.Toolkit.MeshEditor.TreesItems;
using System.Windows;
using System.Windows.Controls;

namespace Red.Toolkit.MeshEditor.Controls
{
    /// <summary>
    /// Interaction logic for MaterialsTreeControl.xaml
    /// </summary>
    public partial class MaterialsTreeControl : UserControl
    {
        private MaterialTreeItemRoot _materialsRootItem;
        private Notifier _listChangedNotifier;

        private DataView _selectionView;

        public MaterialsTreeControl()
        {
            InitializeComponent();
        }

        public void Initialize( DataView rootView )
        {
            _materialsRootItem = new MaterialTreeItemRoot( rootView,
                item =>
                {
                    _materialsTree.ItemsSource = item.Children;
                    item.IsExpanded = true;

                    item.View.RequestNotifier( "materialsListChanged", notifier =>
                    {
                        _listChangedNotifier = notifier;
                        _listChangedNotifier.OnNotifyEvent += ListChangedNotifier_OnNotifyEvent;
                    } );

                    _selectionView = item.View.GetSubView( "selectedMaterial" );
                    InteropDataItem.CreateRoot( _selectionView, rootItem =>
                    {
                        _materialDataCategory.DataContext = rootItem;
                    } );
                } );
        }

        public void Refresh()
        {
            if ( _materialsTree.SelectedItem != null )
            {
                _materialsTree.SelectedItem.IsSelected = false;
            }
            _materialsRootItem.Refresh();
        }

        private void ListChangedNotifier_OnNotifyEvent( string evt )
        {
            _materialsRootItem.Refresh();
        }

        private void CreateNew_Executed( object sender, System.Windows.Input.ExecutedRoutedEventArgs e )
        {
            MaterialTreeItemLeaf node = e.Parameter as MaterialTreeItemLeaf;
            int tmpCounter = node.Parent.Children.Count;
            node.Execute( "addMaterial", "material_" + tmpCounter );
        }

        private void Copy_Executed( object sender, System.Windows.Input.ExecutedRoutedEventArgs e )
        {
            MaterialTreeItemLeaf node = e.Parameter as MaterialTreeItemLeaf;
            int tmpCounter = node.Parent.Children.Count;
            node.Execute( "copyMaterial", "material_" + tmpCounter );
        }

        private void RemoveUnused_Executed( object sender, System.Windows.Input.ExecutedRoutedEventArgs e )
        {
            // remove unused materials
            _materialsRootItem.Execute( "removeUnusedMaterials" );
        }

        private void Remove_Executed( object sender, System.Windows.Input.ExecutedRoutedEventArgs e )
        {
            MaterialTreeItemLeaf node = e.Parameter as MaterialTreeItemLeaf;
            node.Execute( "remove" );
        }

        private void Remove_CanExecute( object sender, System.Windows.Input.CanExecuteRoutedEventArgs e )
        {
            MaterialTreeItemLeaf node = e.Parameter as MaterialTreeItemLeaf;
            e.CanExecute = node.Parent.Children.Count > 1;
        }

        private void MaterialsTree_SelectedItemChanged( object sender, RoutedPropertyChangedEventArgs<object> e )
        {
            MaterialTreeItemLeaf selected = e.NewValue as MaterialTreeItemLeaf;
            if ( selected != null )
            {
                selected.Execute( "select" );
            }
            else
            {
                _materialsRootItem.Execute( "clearSelection" );
            }
        }
    }
}
